unit clsProducao;

interface

uses Dm, dbtables, biblio, sysutils, dbclient, clsEntidade;

type
  TProducao = class(TObject)
  public
    Prod_DataCad: TDateTime;
    Prod_DataPrev: TDateTime;
    Prod_Obs: string;
    Prod_Obs2: string;
    Prod_Nome: string;
    Prod_Fechada: string;
    Prod_Retida: string;
    Prod_DataValidade: TDateTime;
    Prod_Serie: string;
    function Recuperar(Prod_Numero: string): boolean;
    function Retorna_Ultima: string;
    function Retorna_UltimaSerie(pData: string): string;
    procedure Gravar(Prod_Numero: string);
    procedure Excluir(Prod_Numero: string);
    procedure Consultar(Data_Cad: TDateTime; var tq: TQuery);
    function Bol_Finalizado(Prod_Numero: string): Boolean;
    function ProduzidoParcial(Prod_Numero: string): Boolean;
    procedure FechaBol(Prod_Numero: string);
  end;

implementation

uses DB;

procedure TProducao.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from Producao where Prod_Numero=:a');
  tq.params[0].asstring := Prod_Numero;
  tq.open;
  if tq.eof then begin
    tq.close;
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('Insert into Producao values (' +
      ':k_Prod_Numero  ,' +
      ':k_Prod_DataCad ,' +
      ':k_Prod_DataPrev,' +
      ':k_Prod_Obs,' +
      ':k_Prod_Obs2,' +
      ':k_Prod_Nome,' +
      ':k_Prod_Fechada,' +
      ':k_Prod_Retida,' +
      ':k_Prod_DataValidade,' +
      ':k_Prod_Serie )');
  end else begin
    tq.close;
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('Update Producao set            ' +
      'Prod_DataCad =:k_Prod_DataCad ,' +
      'Prod_DataPrev=:k_Prod_DataPrev,' +
      'Prod_Obs     =:k_Prod_Obs,' +
      'Prod_Obs2     =:k_Prod_Obs2,' +
      'Prod_Nome    =:k_Prod_Nome,' +
      'Prod_Fechada =:k_Prod_Fechada, ' +
      'Prod_Retida  =:k_Prod_Retida, ' +
      'Prod_DataValidade =:k_Prod_DataValidade,' +
      'Prod_Serie = :k_Prod_Serie ' +
      'where                          ' +
      'Prod_Numero  =:k_Prod_Numero   ');
  end;
  tq.parambyname('k_Prod_Numero').asstring := Prod_Numero;
  tq.parambyname('k_Prod_DataCad').asdatetime := Prod_DataCad;
  tq.parambyname('k_Prod_DataPrev').asdatetime := Prod_DataPrev;
  tq.parambyname('k_Prod_Obs').asstring := Prod_Obs;
  tq.ParamByName('k_prod_Obs2').AsString := Prod_Obs2;
  tq.ParamByName('k_Prod_Nome').AsString := Prod_Nome;
  tq.ParamByName('k_Prod_Fechada').AsString := Prod_Fechada;
  tq.ParamByName('k_Prod_Retida').AsString := Prod_Retida;
  tq.ParamByName('k_Prod_DataValidade').AsDateTime := Prod_DataValidade;
  tq.ParamByName('k_Prod_Serie').AsString := Prod_Serie;
  tq.execsql;
  tq.close;
  tq.Free;
end;

procedure TProducao.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Delete from Producao where Prod_Numero=:a');
  tq.params[0].asstring := Prod_Numero;
  tq.execsql;

  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Update PedidosVenda set Ped_NumPrev = null where Ped_NumPrev=:a');
  tq.params[0].asstring := Prod_Numero;
  tq.execsql;

  tq.close;
  tq.Free;
end;

function TProducao.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from Producao where Prod_Numero=:a');
  tq.params[0].asstring := Prod_Numero;
  tq.open;
  if tq.eof then result := false else result := true;
  Prod_DataCad := tq.fieldbyname('Prod_DataCad').asdatetime;
  Prod_DataPrev := tq.fieldbyname('Prod_DataPrev').asdatetime;
  Prod_Obs := tq.fieldbyname('Prod_Obs').asstring;
  Prod_Obs2 := tq.fieldbyname('Prod_Obs2').AsString;
  Prod_Nome := tq.FieldByName('Prod_Nome').AsString;
  Prod_Fechada := tq.FieldByName('Prod_Fechada').AsString;
  Prod_Retida := tq.FieldByName('Prod_Retida').AsString;
  Prod_DataValidade := tq.FieldByName('Prod_DataValidade').AsDateTime;
  Prod_Serie := tq.FieldByName('Prod_Serie').AsString;

  tq.close;
  tq.free;
end;

function TProducao.Retorna_Ultima;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select max(Prod_Numero) as ultima from Producao');
  tq.open;
  if tq.fieldbyname('ultima').asstring = '' then
    result := '1'
  else
    result := inttostr(tq.fieldbyname('ultima').asinteger + 1);
  tq.close;
  tq.Free;
end;

procedure TProducao.Consultar;
begin
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from Producao where Prod_DataCad>=:a order by Prod_DataCad,Prod_Numero');
  tq.params[0].asdatetime := Data_Cad;
  tq.open;
end;

function TProducao.Bol_Finalizado(Prod_Numero: string): Boolean;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select distinct p.prod_numero from Producao p, ItemProducao i, produto a, parametros par ' +
    'where p.Prod_Numero = ' + QuotedStr(Prod_Numero) + ' and ' +
    'Itp_Quantidade <> ITP_QTDEPRODUZIDA  ' +
    'and a.pro_codigo = i.pro_codigo and ' +
    'p.prod_numero = i.prod_numero and   ' +
    'par.cod_par=''1'' and               ' +
    'Fase_Codigo = Par.Fase_Nivel and    ' +
    'Itp_Fase is not null                ');
  tq.Open;
  Result := tq.Eof;
  tq.Close;
  tq.Free;
end;

procedure TProducao.FechaBol(Prod_Numero: string);
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Update Producao set Prod_Fechada = ''S'' ' +
    'where Prod_Numero = ' + QuotedStr(Prod_Numero));
  tq.ExecSQL;
  tq.close;
  tq.Free;
end;

function TProducao.ProduzidoParcial(Prod_Numero: string): Boolean;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from ItemProducao where Prod_Numero=:a');
  tq.params[0].asstring := Prod_Numero;
  tq.open;
  Result := False;
  while not tq.Eof do begin
    if tq.FieldByName('Itp_QtdeProduzida').AsFloat <> 0 then
      Result := True;
    tq.Next;
  end;
  tq.Close;
  tq.Free;
end;

function TProducao.Retorna_UltimaSerie(pData: string): string;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('select max(Prod_Serie) as Ult, substr(Prod_DataCad,4,7) as Data_ ' +
    'from Producao ' +
    'where substr(Prod_DataCad,4,7) = substr(to_date(:DataCad),4,7) ' +
    'group by substr(Prod_DataCad,4,7) order by Data_');
  if Trim(tira_PT(pData)) <> '' then begin
    tq.ParamByName('DataCad').AsString := pData;
    tq.Open;
    if tq.FieldByName('Ult').IsNull then
      Result := '0001'
    else
      Result := StrZero(tq.FieldByName('Ult').AsInteger + 1, 4);
  end else begin
    Result := '';
  end;
  tq.Close;
  tq.Free;
end;

end.

